package com.dao.green.cn.databinding01.uis.activities;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import com.dao.green.cn.databinding01.R;
import com.dao.green.cn.databinding01.databinding.AcIndexBinding;
import com.dao.green.cn.databinding01.databinding.ItemIndexListviewBinding;
import com.dao.green.cn.databinding01.uis.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ant on 2017/7/4  20:36
 * <p>
 * <p>
 *
 * @version 1.0.0
 * @auther Ant
 */

public class IndexActivity extends BaseActivity {
    private AcIndexBinding binding;

    private List<String> strList;
    private List<Class<? extends BaseActivity>> clsList;
    private MyAdapter adapter;


    @Override
    public void binding() {
        binding = DataBindingUtil.setContentView(this, R.layout.ac_index);
        strList = new ArrayList<>();
        clsList = new ArrayList<>();
    }

    @Override
    public void initView() {
        adapter = new MyAdapter(this);
        binding.acIndexListView.setAdapter(adapter);
    }

    @Override
    public void initData() {
        strList.add("mainActivity");
        clsList.add(MainActivity.class);
        strList.add("Demo01");
        clsList.add(Demo01.class);
        strList.add("Demo02  alpha");
        clsList.add(Demo02.class);


        // Set data to adapter of listView
        adapter.setData(strList);
    }

    @Override
    public void initListener() {
        binding.acIndexListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(IndexActivity.this, clsList.get(position)));
            }
        });
    }


    class MyAdapter extends BaseAdapter {
        private Context context;
        private LayoutInflater inflater;
        private ItemIndexListviewBinding binding;

        public List<String> data;


        public MyAdapter(Context context) {
            this.context = context;
            this.inflater = LayoutInflater.from(context);
        }

        public void setData(List<String> data) {
            this.data = data;
            this.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return data == null ? 0 : data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                binding = DataBindingUtil.inflate(inflater, R.layout.item_index_listview, parent, false);
                convertView = binding.getRoot();
                convertView.setTag(binding);

            } else {
                binding = (ItemIndexListviewBinding) convertView.getTag();
            }

            // bind data
            binding.setStr(data.get(position));
            return convertView;
        }
    }


}
